package com.ruoyi.project.module.zxy;
        import com.ruoyi.common.core.utils.poi.ExcelUtil;
        import com.ruoyi.common.core.web.controller.BaseController;
        import com.ruoyi.common.core.web.domain.AjaxResult;
        import com.ruoyi.common.core.web.page.TableDataInfo;
        import com.ruoyi.common.log.annotation.Log;
        import com.ruoyi.common.log.enums.BusinessType;
        import com.ruoyi.common.security.annotation.RequiresPermissions;
        import com.ruoyi.project.domain.YqgConsumptionDetails;
        import com.ruoyi.project.domain.ZxyConsumptionDetails;
        import com.ruoyi.project.module.lyx.lei.Chargeback;
        import com.ruoyi.project.module.zxy.lei.Xspz;
        import com.ruoyi.project.service.IYqgBarBillsService;
        import com.ruoyi.project.service.IYqgConsumptionDetailsService;
        import com.ruoyi.project.service.IYqgHandsService;
        import com.ruoyi.project.service.IYqgInformationRoomService;
//        import com.sun.javafx.collections.MappingChange;
//        import com.sun.org.apache.xerces.internal.xs.XSNamedMap;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;

        import javax.servlet.http.HttpServletResponse;
        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

/**
 * 消费详细Controller
 *
 * @author zxy
 * @date 2023-06-15
 */
        @RestController
        @RequestMapping("/zxy/xspz") /**这里是api里的module下lyx里的xspz.js文件*/

        public class ZxyYqgXspzController extends BaseController {


                @Autowired
                private IYqgConsumptionDetailsService yqgConsumptionDetailsService;

                /**
                 * 查询消费详细列表
                 */
           //     @RequiresPermissions("project:details:list")
                @GetMapping("/list")
                public TableDataInfo list(ZxyConsumptionDetails yqgConsumptionDetails)
                {
                        /*List<YqgConsumptionDetails> huizonglist=new ArrayList<>();*/
                        /*String Category=yqgConsumptionDetails.getProjectCategory();
                        System.out.println(yqgConsumptionDetails.getNote1());*/
                       /* if(yqgConsumptionDetails.getProjectCategory()==null){
                                List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsListbyopentime(yqgConsumptionDetails);
                                huizonglist.addAll(list);
                        }
                        else{
                                String[] Catelist=Category.split(",");
                                for(int i=0;i<Catelist.length;i++){
                                        yqgConsumptionDetails.setProjectCategory(Catelist[i]);
                                        System.out.println(yqgConsumptionDetails.getProjectCategory());
                                        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsListbyopentime(yqgConsumptionDetails);
                                        huizonglist.addAll(list);
                                }
                        }*/
                       /* if(yqgConsumptionDetails.getProjectCategory()==null){

                        }
                        else{

                              String[] Catelist=Category.split(",");
                              yqgConsumptionDetails.setProjectCategory(Catelist);
                              System.out.println(yqgConsumptionDetails.getProjectCategory());
                        }*/
                        /*
                        yqgConsumptionDetails.setProjectCategory(null);*/
                        /*Map<YqgConsumptionDetails,String[]> ymap=new HashMap<>();
                        ymap.put(yqgConsumptionDetails,Catelist);*/
                        /*System.out.println(ymap);
                        System.out.println(ymap.get(yqgConsumptionDetails));*/
                        /*startPage();*/

                        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsListbyopentime(yqgConsumptionDetails);
                        Map<String, YqgConsumptionDetails> counts = new HashMap<>();
                        for(int i=0;i<list.size();i++){
                                if(counts.containsKey(list.get(i).getEntryName())){
                                        YqgConsumptionDetails yqg=counts.get(list.get(i).getEntryName());
                                        int count=Integer.valueOf(yqg.getQuantity())+Integer.valueOf(list.get(i).getQuantity());
                                        yqg.setQuantity(String.valueOf(count));
                                        counts.put(list.get(i).getEntryName(),yqg);
                                }else{
                                        counts.put(list.get(i).getEntryName(),list.get(i));
                                }
                        }
                        List<YqgConsumptionDetails> countlist=new ArrayList<>();
                        //过滤重复的商品名
                        // 输出结果 这里是一个hasemap<商品名，商品对象>
                        for (Map.Entry<String, YqgConsumptionDetails> entry : counts.entrySet()) {
                                //这里获得的是hasemap的值
                                YqgConsumptionDetails yqg=entry.getValue();
                                //算合计金额
                                yqg.setTotalAmount(String.valueOf(Integer.valueOf(yqg.getQuantity())*Integer.valueOf(yqg.getUnitPrice())));
                                //算提成价
                                if(yqg.getCommissionPrice()==null){
                                        yqg.setCommissionPrice("0");

                                }else{
                                        double commission_price=Double.valueOf(yqg.getCommissionPrice())*Double.valueOf(yqg.getQuantity());
                                        yqg.setCommissionPrice(String.valueOf(commission_price));
                                }
                                countlist.add(yqg);

                        }
                        //重新进入新的列表
                        List<Xspz> Xspzlist=new ArrayList<>();
                        for (int c = 0; c < countlist.size(); c++) {
                                Xspz xspz=new Xspz();
                                xspz.setProjectCode(countlist.get(c).getProjectCode());
                                xspz.setEntryName(countlist.get(c).getEntryName());
                                xspz.setUnitPrice(countlist.get(c).getUnitPrice());
                                xspz.setQuantity(countlist.get(c).getQuantity());
                                xspz.setTotalAmount(countlist.get(c).getTotalAmount());
                                xspz.setCommissionPrice(countlist.get(c).getCommissionPrice());
                                xspz.setJingEr(String.valueOf(Double.valueOf(countlist.get(c).getTotalAmount())-Double.valueOf(countlist.get(c).getCommissionPrice())));
                                Xspzlist.add(xspz);

                        }

                        /*return getDataTable(countlist);*/
                        return getDataTable(Xspzlist);

                }

                /**
                 * 导出消费详细列表
                 */
                @RequiresPermissions("project:details:export")
                @Log(title = "导出", businessType = BusinessType.EXPORT)
                @PostMapping("/export")
                public void export(HttpServletResponse response, ZxyConsumptionDetails yqgConsumptionDetails)
                {
                        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsListbyopentime(yqgConsumptionDetails);
                        ExcelUtil<Xspz> util = new ExcelUtil<Xspz>(Xspz.class);
                        Map<String, YqgConsumptionDetails> counts = new HashMap<>();
                        for(int i=0;i<list.size();i++){
                                if(counts.containsKey(list.get(i).getEntryName())){
                                        YqgConsumptionDetails yqg=counts.get(list.get(i).getEntryName());
                                        int count=Integer.valueOf(yqg.getQuantity())+Integer.valueOf(list.get(i).getQuantity());
                                        yqg.setQuantity(String.valueOf(count));
                                        counts.put(list.get(i).getEntryName(),yqg);
                                }else{
                                        counts.put(list.get(i).getEntryName(),list.get(i));
                                }
                        }
                        List<YqgConsumptionDetails> countlist=new ArrayList<>();
                        // 输出结果


                        for (Map.Entry<String, YqgConsumptionDetails> entry : counts.entrySet()) {
                                YqgConsumptionDetails yqg=entry.getValue();
                                yqg.setTotalAmount(String.valueOf(Integer.valueOf(yqg.getQuantity())*Integer.valueOf(yqg.getUnitPrice())));
                                if(yqg.getCommissionPrice()==null){
                                        yqg.setCommissionPrice("0");

                                }else{
                                        double commission_price=Double.valueOf(yqg.getCommissionPrice())*Double.valueOf(yqg.getQuantity());
                                        yqg.setCommissionPrice(String.valueOf(commission_price));
                                }
                                countlist.add(yqg);
                        }
                        int quantity=0;
                        int Total=0;
                        double Commission=0;
                        double JingEr=0;
                        List<Xspz> Xspzlist=new ArrayList<>();
                        for (int c = 0; c < countlist.size(); c++) {
                                quantity=quantity+Integer.valueOf(countlist.get(c).getQuantity());
                                Total=Total+Integer.valueOf(countlist.get(c).getTotalAmount());
                                Commission=Commission+Double.valueOf(countlist.get(c).getCommissionPrice());
                                Xspz xspz=new Xspz();
                                xspz.setProjectCode(countlist.get(c).getProjectCode());
                                xspz.setEntryName(countlist.get(c).getEntryName());
                                xspz.setUnitPrice(countlist.get(c).getUnitPrice());
                                xspz.setQuantity(countlist.get(c).getQuantity());
                                xspz.setTotalAmount(countlist.get(c).getTotalAmount());
                                xspz.setCommissionPrice(countlist.get(c).getCommissionPrice());
                                xspz.setJingEr(String.valueOf(Double.valueOf(countlist.get(c).getTotalAmount())-Double.valueOf(countlist.get(c).getCommissionPrice())));
                                JingEr=JingEr+Double.valueOf(xspz.getJingEr());
                                Xspzlist.add(xspz);

                        }
                        Xspz xspzheji=new Xspz();
                        xspzheji.setEntryName("合计");
                        xspzheji.setQuantity(String.valueOf(quantity));
                        xspzheji.setTotalAmount(String.valueOf(Total));
                        xspzheji.setCommissionPrice(String.valueOf(Commission));
                        xspzheji.setJingEr(String.valueOf(JingEr));
                        Xspzlist.add(xspzheji);
                        util.exportExcel(response, Xspzlist, "消费详细数据");
                }

                /**
                 * 获取消费详细详细信息
                 */
                @RequiresPermissions("project:details:query")
                @GetMapping(value = "/{ID}")
                public AjaxResult getInfo(@PathVariable("ID") Long ID)
                {
                        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
                }

                /**
                 * 新增消费详细
                 */
                @RequiresPermissions("project:details:add")
                @Log(title = "新增", businessType = BusinessType.INSERT)
                @PostMapping
                public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
                {
                        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
                }

                /**
                 * 修改消费详细
                 */
                @RequiresPermissions("project:details:edit")
                @Log(title = "修改", businessType = BusinessType.UPDATE)
                @PutMapping("/pp")
                public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
                {
                        System.out.println("1000000");
                        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
                }

                /**
                 * 删除消费详细
                 */
                @RequiresPermissions("project:details:remove")
                @Log(title = "删除", businessType = BusinessType.DELETE)
                @DeleteMapping("/{IDs}")
                public AjaxResult remove(@PathVariable Long[] IDs)
                {
                        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
                }




        }
